package controllers

import (
	"encoding/json"
	"time"
	"weiboxs.com/new_h_glasses/enums"
	"weiboxs.com/new_h_glasses/models"
	"weiboxs.com/new_h_glasses/utils"
)

type OutpatientController struct {
	BaseController
}

func (c *OutpatientController) Prepare() {
	c.BaseController.Prepare()
	c.Data["activeSidebarUrl"] = "/outpatient/index"
}

func(c *OutpatientController) Index() {

	c.display("", "outpatient/index_footerjs.html")
}
func (c *OutpatientController) View() {

	c.checkAuthor("outpatient/index") //直接给列表权限
	id, _ := c.GetInt64(":id")
	c.userinfo(id)

	c.Data["nowDate"] = utils.GetDateFormat(time.Now().Unix(), "2006-01-02")

	tabCard := c.GetString("tab_card", "f1")
	c.Data["tab_card"] = tabCard

	switch tabCard {
	case "f1":
		//c.Data["activeSidebarUrl"] = "/outpatient/index"
		c.Data["c"] = c.getOptions()
		c.display("", "outpatient/view_footerjs.html")
	case "f2":
		//c.Data["activeSidebarUrl"] = "/optometry/edit"
		c.optometry(id)
	case "f3":
		//c.Data["activeSidebarUrl"] = "/outpatient/index"
		c.viewInspect(id)
	case "f4":
		//c.Data["activeSidebarUrl"] = "/retail/add"
		c.retails(id)
	case "f5":
		//c.Data["activeSidebarUrl"] = "/retail/add"
		c.followup(id)
	}
	c.Data["activeSidebarUrl"] = "/outpatient/index"
	return
}
//验光信息
func (c *OutpatientController) optometry(id int64) {

	c.display("outpatient/optometry.html", "outpatient/optometry_footerjs.html")
}

//检查记录
func(c *OutpatientController) viewInspect(id int64) {
	c.display("outpatient/viewinspect.html", "outpatient/viewinspect_footerjs.html")
}

//销售记录
func (c *OutpatientController) retails(id int64) {

	c.display("outpatient/retails.html", "outpatient/retails_footerjs.html")
}
//新增随访
func (c *OutpatientController) followup(id int64) {

	c.display("outpatient/followup.html", "outpatient/followup_footerjs.html")
}

//检查
func(c *OutpatientController) Inspect() {
	c.checkAuthor("outpatient/inspect") //直接给列表权限

	c.Data["tab_card"] = "f3"
	c.Data["activeSidebarUrl"] = "/outpatient/inspect"
	c.display("outpatient/inspect.html", "outpatient/inspect_footerjs.html")
}

//POST
func(c *OutpatientController) PostInspect() {
	c.checkAuthor("outpatient/index") //直接给列表权限

	var params models.InspectsQueryParam
	json.Unmarshal(c.Ctx.Input.RequestBody, &params)

	data, total := models.InspectsPageList(&params)
	c.jsonDataTable(total, data)
}

//获取问诊信息
func (c *OutpatientController) GetInterrogations() {
	id, _ := c.GetInt64(":id")
	if id == 0 {
		c.jsonResult(enums.JRCodeSucc, "参数传递错误", "")
	}

	data := models.InterrogationsView(id)

	c.jsonResult(enums.JRCodeSucc, "ok", data)
}

//保存问诊信息
func (c *OutpatientController) SaveInterrogations() {

	interrogations := new(models.Interrogations)

	//slice绑定
	c.Ctx.Input.Bind(&interrogations.BeforeConclusionSlice, "BeforeConclusion")
	c.Ctx.Input.Bind(&interrogations.BeforeTreatmentSlice, "BeforeTreatment")
	c.Ctx.Input.Bind(&interrogations.PastHistorySlice, "PastHistory")

	c.Ctx.Input.Bind(&interrogations.EnvironmentSlice, "Environment")
	c.Ctx.Input.Bind(&interrogations.RefractionOdSlice, "RefractionOd")
	c.Ctx.Input.Bind(&interrogations.RefractionOsSlice, "RefractionOs")

	c.Ctx.Input.Bind(&interrogations.AmblyopiaOdSlice, "AmblyopiaOd")
	c.Ctx.Input.Bind(&interrogations.AmblyopiaOsSlice, "AmblyopiaOs")
	c.Ctx.Input.Bind(&interrogations.ProposalSlice, "Proposal")

	c.Ctx.Input.Bind(&interrogations.InheritanceSlice, "Inheritance")

	if err := c.ParseForm(interrogations); err != nil {
		c.jsonResult(enums.JRCodeFailed, err.Error(), "")
	}
	arcid ,_ := c.GetInt64("Archives")
	archmodel := new(models.Archives)
	archmodel.Id = arcid
	interrogations.Archives = archmodel

	if err := models.InterrogationsSave(interrogations); err != nil {
		c.jsonResult(enums.JRCodeFailed, err.Error(), "")
	}
	c.jsonResult(enums.JRCodeSucc, "问诊保存成功", "")
}


func (c *OutpatientController) userinfo(id int64) {
	m, _ := models.GetArchivesById(id)
	c.Data["m"] = m
	//生成档案编号
	c.Data["number"] = models.CreateInterrogationsNumber(m.Id)
}

//获取配置信息
func (c *OutpatientController) getOptions() (o map[string]interface{}) {

	o = make(map[string]interface{})

	f := models.OptionsQueryParam{}
	f.Status = "1"
	f.Depid = c.curUser.DepartmentId

	//获取医生
	f.Typeid = "10"
	o["doctor"] = models.OptionsList(&f)

	f.Typeid = "11"
	o["optometrist"] = models.OptionsList(&f)

	return
}

//保存检查项目
func (c *OutpatientController) SaveInspect() {
	product_name := c.GetString("product_name")
	unit_price,_  :=  c.GetFloat("unit_price");

	model := &models.Inspects{}
	model.ProductName = product_name
	model.UnitPrice = unit_price
	if _, err := models.InspectsAdd(model); err != nil {
		c.jsonResult(enums.JRCodeFailed, err.Error(), "")
	}

	c.jsonResult(enums.JRCodeSucc, "ok", "")
}

//删除检查项目
func (c *OutpatientController) DeleteInspect() {

	id,_ := c.GetInt64("id")
	if id == 0 {
		c.jsonResult(enums.JRCodeFailed, "请选择检查项目", "")
	}

	if err := models.InspectsDeleteById(id); err != nil {
		c.jsonResult(enums.JRCodeFailed, err.Error(), "")
	}

	c.jsonResult(enums.JRCodeSucc, "ok", "")
}
